﻿Imports DataSet_ohlTableAdapters
Imports DataSet_ohl
Imports System.Data

Partial Class Registro_Programa
    Inherits System.Web.UI.Page
    Private dataSet As DataSet

    Dim TA_Animal, TA_Crias As New animalTableAdapter
    Dim DT_Animal, DT_Crias As New animalDataTable

    Dim TA_Empadre, TA_tempE As New empadreTableAdapter
    Dim DT_Empadre, DT_tempE As New empadreDataTable

    Dim TA_EmpMov, TA_tempEM As New Emp_MovsTableAdapter
    Dim DT_EmpMov, DT_tempEM As New Emp_MovsDataTable

    Dim xAnimal, xNumEmp, xStatus As String

    Protected Sub Registro_Programa_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'If (idAnimal <> 0) And Not IsPostBack Then ' Cargar la informacion a traves de la tabla
        If Not IsPostBack Then
            pnlEmpadre.Visible = False
            pnlEmpMov.Visible = False
            Dim I As Decimal
            For I = 1 To 25 Step 0.5
                ddlEdadT.Items.Add(I)
                ddlEdadV.Items.Add(I)
            Next
            ddlEdadT.SelectedIndex = 3
            ddlEdadV.SelectedIndex = 3

            For I = Year(Now) To 2006 Step -1
                ddlAno.Items.Add(I)
            Next
            ddlAno.SelectedIndex = 0
            ddlAno_SelectedIndexChanged(ddlAno, e)
        End If
    End Sub

    Protected Sub ddlAno_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAno.SelectedIndexChanged
        DT_Empadre = TA_Empadre.GetEmpadreByAnoN(ddlAno.SelectedValue)
        gvEmpadre.DataSource = DT_Empadre
        gvEmpadre.DataBind()
        gvEmpMov.DataSource = Nothing
        gvEmpMov.DataBind()
        gvVacas.DataSource = Nothing
        gvVacas.DataBind()
        btnGrabaSelecc.Visible = False
        chkPropV.Visible = False
        'gvCrias.DataSource = Nothing
        'gvCrias.DataBind()
        'txtCrias.Text = ""
        lblTitulo.Text = "E M P A D R E S    " & ddlAno.SelectedValue.ToString
        lblError.Text = ""
    End Sub

    Protected Sub imgbtnAno_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnAno.Click
        ddlAno.Items.Insert(0, ddlAno.Items(0).Value + 1)
        ddlAno.SelectedIndex = 0
        ddlAno_SelectedIndexChanged(ddlAno, e)
    End Sub

    Protected Sub ddlEdadV_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlEdadV.SelectedIndexChanged
        If chkPropV.Visible = True Then
            chkPropV_CheckedChanged(chkPropV, e)
        End If
    End Sub

    Protected Sub gvEmpadre_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvEmpadre.SelectedIndexChanged
        'Response.Write("Seleccion antes:" & gvEmpadre.SelectedRow.Cells(1).Text)
        'txtIndexEmp.Text = gvEmpadre.SelectedRow.RowIndex
        DT_EmpMov = TA_EmpMov.Get_EM_ByEmp(gvEmpadre.DataKeys(gvEmpadre.SelectedIndex).Item("empadreId"))
        gvEmpMov.DataSource = DT_EmpMov
        gvEmpMov.DataBind()
        DT_Animal = TA_Animal.GetAniEmpByVaca1(ddlEdadV.SelectedValue, CType(ddlAno.SelectedValue, Integer))
        gvVacas.DataSource = DT_Animal.Select()
        gvVacas.DataBind()
        txtIndexEmp.Text = gvEmpadre.SelectedIndex
        btnGrabaSelecc.Visible = True
        chkPropV.Visible = True
        chkPropV.Checked = True
        If DT_EmpMov.Rows.Count <= 0 Then lblError.Text = "NINGUNA VACA ASIGNADA"
        If DT_EmpMov.Rows.Count > 0 Then lblError.Text = ""
        lblTitulo.Text = "E M P A D R E S    " & ddlAno.SelectedValue.ToString
        'xAnimal = "ID_Padre = " & gvEmpadre.SelectedRow.Cells(6).Text
        'txtCrias.Text = "Animal : " & gvEmpadre.SelectedRow.Cells(4).Text
        'ChecaCrias(1)
    End Sub

    Protected Sub gvEmpadre_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvEmpadre.RowEditing
        'Dim grid As New GridView
        'grid = sender
        'txtSalida.Text = "Ed. RowEditing " & gvEmpadre.Rows(e.NewEditIndex).Cells(4).Text
        xStatus = "E"
        txtIndexEmp.Text = e.NewEditIndex
        pnlParte1.Visible = False
        pnlEmpadre.Visible = True
        lblNumEmpT.Text = gvEmpadre.DataKeys(Convert.ToInt32(txtIndexEmp.Text)).Item("empadreId")
        CargaEmpadre()
    End Sub

    Protected Sub gvEmpadre_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvEmpadre.RowDeleting
        Dim actualizado As Integer ' Numero de registros actualizados
        'txtSalida.Text = "Borrado RowDeleting : " & gvEmpadre.Rows(e.RowIndex).Cells(3).Text
        xNumEmp = gvEmpadre.DataKeys(e.RowIndex).Item("empadreId")
        'txtIndexEmp.Text = e.RowIndex
        DT_tempE = TA_tempE.GetEmpadreById(CType(xNumEmp, Integer))
        If DT_tempE.Rows.Count > 0 Then actualizado = TA_tempE.Delete(CType(xNumEmp, Integer))
        lblError.Text = actualizado.ToString + " REGISTRO BORRADO"
        BorraEmpMov()
        ddlAno_SelectedIndexChanged(ddlAno.SelectedValue, e)
    End Sub

    Protected Sub gvEmpadre_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvEmpadre.PageIndexChanging
        gvEmpadre.PageIndex = e.NewPageIndex
        gvEmpadre.DataBind()
        gvEmpMov.DataSource = Nothing
        gvEmpMov.DataBind()
    End Sub

    Protected Sub gvEmpMov_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvEmpMov.SelectedIndexChanged
        txtSalida.Text = "Seleccion V : " & gvEmpMov.SelectedRow.Cells(3).Text & "-" & gvEmpMov.SelectedRow.Cells(6).Text
        xAnimal = "ID_Madre = " & gvEmpMov.SelectedRow.Cells(6).Text
        'txtCrias.Text = "Animal : " & gvEmpMov.SelectedRow.Cells(4).Text
        'ChecaCrias(1)
    End Sub

    Protected Sub gvEmpMov_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvEmpMov.RowEditing
        'txtSalida.Text = "Edicion RowEditing " & gvEmpMov.Rows(e.NewEditIndex).Cells(3).Text
        xStatus = "E"
        pnlParte1.Visible = False
        pnlEmpadre.Visible = False
        pnlEmpMov.Visible = True
        lblNumEmpV.Text = gvEmpMov.DataKeys(e.NewEditIndex).Item("empadreId")
        lblNumEmpT.Text = gvEmpMov.DataKeys(e.NewEditIndex).Item("empadreId")
        lblVacaId.Text = gvEmpMov.DataKeys(e.NewEditIndex).Item("vacaId")
        'lblVacaId.Text = gvEmpMov.Rows(e.NewEditIndex).Cells(6).Text
        CargaEmpMov()
    End Sub

    Protected Sub gvEmpMov_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvEmpMov.RowDeleting
        Dim actualizado As Integer ' Numero de registros actualizados
        txtSalida.Text = "Borrado RowDeleting : " & gvEmpMov.Rows(e.RowIndex).Cells(3).Text
        xNumEmp = gvEmpMov.Rows(e.RowIndex).Cells(3).Text

        DT_tempEM = TA_tempEM.Get_EM_ByEmpAni(CType(xNumEmp, Integer), CType(gvEmpMov.Rows(e.RowIndex).Cells(6).Text, Integer))
        If DT_tempEM.Rows.Count > 0 Then actualizado = TA_tempEM.Delete(CType(xNumEmp, Integer), CType(gvEmpMov.Rows(e.RowIndex).Cells(6).Text, Integer))
        lblError.Text = actualizado.ToString + " REGISTRO BORRADO"
        gvEmpadre_SelectedIndexChanged(txtIndexEmp.Text, e)
        'DT_EmpMov = TA_EmpMov.Get_EM_ByEmp(CType(xNumEmp, Integer))
        'gvEmpMov.DataSource = DT_EmpMov
        'gvEmpMov.DataBind()
    End Sub

    Protected Sub gvEmpMov_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvEmpMov.PageIndexChanging
        gvEmpMov.PageIndex = e.NewPageIndex
        gvEmpMov.DataBind()
    End Sub

    Protected Sub gvToros_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvToros.SelectedIndexChanged
        pnlEmpadre.Visible = True
        'txtToro.Text = gvToros.SelectedRow.Cells(2).Text + gvToros.SelectedRow.Cells(3).Text
        'lblToroId.Text = gvToros.SelectedRow.Cells(5).Text
        'lblNomToro.Text = gvToros.SelectedRow.Cells(4).Text
        txtToro.Text = "T" & gvToros.DataKeys(gvToros.SelectedIndex).Item("cveAnimal")
        lblToroId.Text = gvToros.DataKeys(gvToros.SelectedIndex).Item("animalID")
        lblNomToro.Text = gvToros.DataKeys(gvToros.SelectedIndex).Item("nombre")
    End Sub

    Protected Sub gvToros_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvToros.RowEditing
        'txtSalida.Text = "Seleccion SEM : " & gvToros.Rows(e.NewEditIndex).Cells(3).Text & "-" & gvToros.Rows(e.NewEditIndex).Cells(5).Text
        'xAnimal = "ID_Padre = " & gvToros.Rows(e.NewEditIndex).Cells(5).Text
        'txtSalida.Text = gvToros.Rows(e.NewEditIndex).Cells(3).Text
        'ChecaCrias(2)
    End Sub

    Protected Sub gvToros_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvToros.PageIndexChanging
        gvToros.PageIndex = e.NewPageIndex
        gvToros.DataBind()
    End Sub

    Protected Sub gvVacas_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvVacas.SelectedIndexChanged
        'pnlEmpadre.Visible = True
        'pnlEmpMov.Visible = True
        txtVaca.Text = "V" & gvVacas.DataKeys(gvVacas.SelectedIndex).Item("cveAnimal")
        lblVacaId.Text = gvVacas.DataKeys(gvVacas.SelectedIndex).Item("animalID")
        lblNomVaca.Text = gvVacas.DataKeys(gvVacas.SelectedIndex).Item("nombre")
    End Sub

    Protected Sub gvVacas_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvVacas.RowEditing
        'txtSalida.Text = "Seleccion VAC : " & gvVacas.Rows(e.NewEditIndex).Cells(3).Text & "-" & gvVacas.Rows(e.NewEditIndex).Cells(5).Text
        'xAnimal = "ID_Madre = " & gvVacas.Rows(e.NewEditIndex).Cells(5).Text
        'txtSalida.Text = gvVacas.Rows(e.NewEditIndex).Cells(3).Text
        'ChecaCrias(2)
    End Sub

    Protected Sub gvVacas_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvVacas.PageIndexChanging
        gvVacas.PageIndex = e.NewPageIndex
        gvVacas.DataBind()
    End Sub

    Protected Sub btnEmpadre_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEmpadre.Click
        xStatus = "A"
        pnlParte1.Visible = False
        pnlEmpadre.Visible = True
        CargaEmpadre()
        'txtToro.Focus()
    End Sub

    Protected Sub btnGrabaSelecc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGrabaSelecc.Click
        Dim iRegistrosActualizados As Integer = 0 ' Cuenta el numero de registros modificados
        Dim bSelecciono As Boolean = False

        For Each row In gvVacas.Rows
            Dim chk As CheckBox = row.FindControl("chkItemSelect1") '+sIndice

            If chk IsNot Nothing AndAlso chk.Checked Then 'Si está seleccionado obten datos del Animal
                Dim xIdAnimal As Integer = Convert.ToInt32(gvVacas.DataKeys(row.RowIndex).Item("animalID"))
                Dim xcveAnimal As String = gvVacas.DataKeys(row.RowIndex).Item("cveAnimal")
                Dim xnomAnimal As String = gvVacas.DataKeys(row.RowIndex).Item("nombre")
                Dim xIdEmp As Integer = Convert.ToInt32(gvEmpadre.DataKeys(CType(txtIndexEmp.Text, Integer)).Item("empadreId"))
                Dim xFIemp, xFFemp As Nullable(Of Date)
                'CONVERT(date, @f_movto, 103))
                Try
                    xFIemp = gvEmpadre.DataKeys(CType(txtIndexEmp.Text, Integer)).Item("fechaini")
                Catch ex As Exception
                    'xFIemp = "01/01/1900"
                End Try
                Try
                    xFFemp = gvEmpadre.DataKeys(CType(txtIndexEmp.Text, Integer)).Item("fechafin")
                Catch ex As Exception
                    'xFFemp = "01/01/1900"
                End Try

                iRegistrosActualizados = iRegistrosActualizados + TA_tempEM.Insert(xIdEmp, xcveAnimal, xIdAnimal, xnomAnimal, ddlAno.SelectedValue, xFIemp, xFFemp, 0, 0)
                bSelecciono = True
            End If
        Next

        If Not bSelecciono Then 'ERROR: No hay selección
            lblError.Text = "Selecciona al menos una vaca"
        Else
            gvEmpadre_SelectedIndexChanged(txtIndexEmp.Text, e)
            lblError.CssClass = "MaskedEditFocus"
            lblError.Text = "Registros agregados: " + iRegistrosActualizados.ToString
        End If
    End Sub

    Protected Sub btnGraba1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGraba1.Click
        If txtToro.Text.Length = 0 Then
            'txtToro.Focus()
        Else
            'pnlEmpadre.Visible = True
            btnGraba1.Enabled = True    'IIf(lblNumEmpT.Text = "0", False, True)
            btnCancela1.Enabled = True  'IIf(lblNumEmpT.Text = "0", False, True)
            GrabaEmpadre()
            'If lblNumEmpT.Text = "0" Then
            'gvToros.DataSource = Nothing
            'gvToros.DataBind()
            ''EXTRAE CONSECUTIVO EMPADRE
            'lblNumEmpV.Text = TA_Empadre.SQNumEmp().ToString
            'xStatus = "A"
            'CargaEmpMov()
            'txtVaca.Focus()
            'Else
            Restaura()
            ddlAno_SelectedIndexChanged(ddlAno.SelectedValue, e)
            'End If
        End If
    End Sub

    Protected Sub btnCancela1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancela1.Click
        Restaura()
        ddlAno_SelectedIndexChanged(ddlAno.SelectedValue, e)
    End Sub

    Protected Sub btnGraba2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGraba2.Click
        If txtVaca.Text.Length = 0 Then
            'txtVaca.Focus()
        Else
            'If es Modo Edicion . . . 
            If lblNumEmpT.Text = lblNumEmpV.Text Then
                GrabaEmpMov()
                Restaura()
                gvEmpadre_SelectedIndexChanged(txtIndexEmp.Text, e)
                'DT_EmpMov = TA_EmpMov.Get_EM_ByEmp(CType(lblNumEmpV.Text, Integer))
                'gvEmpMov.DataSource = DT_EmpMov
                'gvEmpMov.DataBind()
            Else
                pnlEmpadre.Visible = True
                pnlEmpMov.Visible = True
                btnGraba1.Enabled = False
                btnCancela1.Enabled = False
                GrabaEmpMov()
                xStatus = "A"
                CargaEmpMov()
                txtVaca.Focus()
            End If
        End If
    End Sub

    Protected Sub btnCancela2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancela2.Click
        Restaura()
        gvEmpadre_SelectedIndexChanged(txtIndexEmp.Text, e)
        'ddlAno_SelectedIndexChanged(ddlAno.SelectedValue, e)
    End Sub

    Private Sub Restaura()
        pnlParte1.Visible = True
        pnlEmpadre.Visible = False
        pnlEmpMov.Visible = False
        btnGraba1.Enabled = True
        btnCancela1.Enabled = True

        btnEmpadre.Visible = True
        gvToros.DataSource = Nothing
        gvToros.DataBind()
        'gvVacas.DataSource = Nothing
        'gvVacas.DataBind()
        'lblTitulo.Text = "E M P A D R E S"
    End Sub

    Private Sub CargaEmpadre()
        'lblNumEmpT.Text = 0
        txtToro.Text = ""
        lblToroId.Text = ""
        lblNomToro.Text = "."
        txtFIT.Text = ""
        txtFFT.Text = ""
        ddlGanCor.SelectedIndex = 0
        ddlUbicacion.SelectedIndex = 0
        chkPropT.Checked = IIf(xStatus = "A", True, False)
        chkPropT.Visible = IIf(xStatus = "A", True, False)
        If xStatus = "A" Then
            lblNumEmpT.Text = 0
            DT_Animal = TA_Animal.GetAniEmpByToro1(ddlEdadT.SelectedValue, CType(ddlAno.SelectedValue, Integer))
            gvToros.DataSource = DT_Animal.Select()
            gvToros.DataBind()
            lblTitulo.Text = "ALTA EMPADRE  " & ddlAno.SelectedValue.ToString
        Else
            DT_tempE = TA_tempE.GetEmpadreById(CType(lblNumEmpT.Text, Integer))
            If DT_tempE.Rows.Count > 0 Then
                txtToro.Text = DT_tempE(0).toro
                lblToroId.Text = DT_tempE(0).toroId
                lblNomToro.Text = DT_tempE(0).nombre
                Try
                    txtFIT.Text = DT_tempE(0).fechaini
                Catch ex As Exception
                    txtFIT.Text = ""
                End Try
                Try
                    txtFFT.Text = DT_tempE(0).fechafin
                Catch ex As Exception
                    txtFFT.Text = ""
                End Try
                ddlGanCor.SelectedValue = DT_tempE(0).gan_cor
                Try
                    ddlUbicacion.SelectedValue = DT_tempE(0).ubicacion
                Catch ex As Exception
                    ddlUbicacion.SelectedIndex = 0
                End Try
            End If
            gvToros.DataSource = Nothing
            gvToros.DataBind()
            lblTitulo.Text = "EDICION EMPADRE  " & txtToro.Text
            'lblAnimal.Text = morfologia(0).claveSNLA + " " + morfologia(0).nombre
            'ddlLongitud.SelectedValue = CType(morfologia(0).piton_long, String)
        End If
    End Sub

    Private Sub CargaEmpMov()
        'Se agrego para validar campos fechas de empadre
        If xStatus = "E" Then CargaEmpadre()

        If txtFIT.Text.Length = 0 Then
            txtFIV.Enabled = False
            ImgBntCal3.Enabled = False
        Else
            txtFIV.Enabled = True
            ImgBntCal3.Enabled = True
        End If
        If txtFFT.Text.Length = 0 Then
            txtFFV.Enabled = False
            ImgBntCal4.Enabled = False
        Else
            txtFFV.Enabled = True
            ImgBntCal4.Enabled = True
        End If
        lblFechas.Text = txtFIT.Text + " AL " + txtFFT.Text
        chkPropVV.Checked = IIf(xStatus = "A", True, False)
        chkPropVV.Visible = IIf(xStatus = "A", True, False)


        txtVaca.Text = ""
        lblNomVaca.Text = "."
        txtFIV.Text = ""
        txtFFV.Text = ""
        If xStatus = "A" Then
            'lblNumEmpV.Text = 0
            'lblVacaId.Text = ""
            DT_Animal = TA_Animal.GetAniEmpByVaca1(ddlEdadV.SelectedValue, CType(ddlAno.SelectedValue, Integer))
            gvVacas2.DataSource = DT_Animal.Select()
            gvVacas2.DataBind()
            gvCrias2.DataSource = Nothing
            gvCrias2.DataBind()
            lblTitulo.Text = "ALTA PIE DE CRIA"
        Else
            DT_tempEM = TA_tempEM.Get_EM_ByEmpAni(CType(lblNumEmpV.Text, Integer), CType(lblVacaId.Text, Integer))
            If DT_tempEM.Rows.Count > 0 Then
                txtVaca.Text = DT_tempEM(0).vaca
                lblNomVaca.Text = DT_tempEM(0).nombre
                Try
                    txtFIV.Text = DT_tempEM(0).fechaini
                Catch ex As Exception
                    txtFIV.Text = ""
                End Try
                Try
                    txtFFV.Text = DT_tempEM(0).fechafin
                Catch ex As Exception
                    txtFFV.Text = ""
                End Try
            End If
            lblTitulo.Text = "EDICION PIE DE CRIA"
        End If
    End Sub

    Private Sub GrabaEmpadre()
        Dim actualizado As Integer ' Numero de registros actualizados
        'If Not IsNothing(morfologia) Then
        DT_tempE = TA_tempE.GetEmpadreById(CType(lblNumEmpT.Text, Integer))
        If DT_tempE.Rows.Count > 0 Then
            DT_tempE(0).toro = txtToro.Text
            DT_tempE(0).toroId = CType(lblToroId.Text, Integer)
            DT_tempE(0).nombre = lblNomToro.Text
            DT_tempE(0).ano = CType(ddlAno.SelectedValue, Integer)
            If txtFIT.Text = "" Then
                DT_tempE(0).SetfechainiNull()
            Else
                DT_tempE(0).fechaini = txtFIT.Text
            End If
            If txtFFT.Text = "" Then
                DT_tempE(0).SetfechafinNull()
            Else
                DT_tempE(0).fechafin = txtFFT.Text
            End If
            DT_tempE(0).gan_cor = ddlGanCor.SelectedValue
            DT_tempE(0).ubicacion = ddlUbicacion.SelectedValue
            actualizado = TA_tempE.Update(DT_tempE)
            txtSalida.Text = actualizado.ToString + " REGISTRO ACTUALIZADO"
        Else
            'Dim xfecha1, xfecha2 As DateTime
            'xfecha1 = Global.System.Convert.DBNull
            'TA_tempE.Insert(txtToro.Text, 999, "NOM2 TORO", 2010, Global.System.Convert.DBNull, xfecha2, txtGC.Text, txtUbicacion.Text, txtEstatus.Text)
            'Row_Emp.toroId = CType(lblToroId.Text, Integer)

            Dim DT_t As New empadreDataTable
            Dim Row_Emp As empadreRow
            Row_Emp = DT_t.NewempadreRow()

            Row_Emp.empadreId = 0
            Row_Emp.toro = txtToro.Text
            Row_Emp.toroId = CType(lblToroId.Text, Integer)
            Row_Emp.nombre = lblNomToro.Text
            Row_Emp.ano = CType(ddlAno.SelectedValue, Integer)
            If txtFIT.Text = "" Then
                Row_Emp.SetfechainiNull()
            Else
                Row_Emp.fechaini = txtFIT.Text
            End If
            If txtFFT.Text = "" Then
                Row_Emp.SetfechafinNull()
            Else
                Row_Emp.fechafin = txtFFT.Text
            End If
            Row_Emp.gan_cor = ddlGanCor.SelectedValue
            Row_Emp.ubicacion = ddlUbicacion.SelectedValue
            Row_Emp.estatus = "N"

            DT_t.AddempadreRow(Row_Emp)
            actualizado = TA_tempE.Update(Row_Emp)
            'actualizado = TA_tempE.UpdateEmpadre()
            txtSalida.Text = actualizado.ToString + " REGISTRO AGREGADO"
        End If
    End Sub

    Private Sub GrabaEmpMov()
        Dim actualizado As Integer ' Numero de registros actualizados
        DT_tempEM = TA_tempEM.Get_EM_ByEmpAni(CType(lblNumEmpV.Text, Integer), CType(lblVacaId.Text, Integer))
        If DT_tempEM.Rows.Count > 0 Then
            DT_tempEM(0).vaca = txtVaca.Text
            DT_tempEM(0).nombre = lblNomVaca.Text
            DT_tempEM(0).ano = CType(ddlAno.SelectedValue, Integer)
            If txtFIV.Text = "" Then
                DT_tempEM(0).SetfechainiNull()
            Else
                DT_tempEM(0).fechaini = txtFIV.Text
            End If
            If txtFFV.Text = "" Then
                DT_tempEM(0).SetfechafinNull()
            Else
                DT_tempEM(0).fechafin = txtFFV.Text
            End If
            actualizado = TA_tempEM.Update(DT_tempEM)
            txtSalida.Text = actualizado.ToString + " REGISTRO ACTUALIZADO"
        Else
            Dim DT_tEM As New Emp_MovsDataTable
            Dim Row_EmpMov As Emp_MovsRow
            Row_EmpMov = DT_tEM.NewEmp_MovsRow()

            Row_EmpMov.empadreId = lblNumEmpV.Text
            Row_EmpMov.vaca = txtVaca.Text
            Row_EmpMov.vacaId = CType(lblVacaId.Text, Integer)
            Row_EmpMov.nombre = lblNomVaca.Text
            Row_EmpMov.ano = CType(ddlAno.SelectedValue, Integer)
            If txtFIV.Text = "" Then
                Row_EmpMov.SetfechainiNull()
            Else
                Row_EmpMov.fechaini = txtFIV.Text
            End If
            If txtFFV.Text = "" Then
                Row_EmpMov.SetfechafinNull()
            Else
                Row_EmpMov.fechafin = txtFFV.Text
            End If

            DT_tEM.AddEmp_MovsRow(Row_EmpMov)
            actualizado = TA_tempEM.Update(Row_EmpMov)
            txtSalida.Text = actualizado.ToString + " REGISTRO AGREGADO"
        End If
    End Sub

    Private Sub ChecaCrias(ByVal xCrias As Integer)
        DT_Crias = TA_Crias.GetAnimal
        If xCrias = 1 Then
            'gvCrias.DataSource = DT_Crias.Select(xAnimal)
            'gvCrias.DataBind()
        Else
            gvCrias2.Columns.Item(3).HeaderText = "CRIAS"
            gvCrias2.Columns.Item(4).HeaderText = txtSalida.Text
            gvCrias2.DataSource = DT_Crias.Select(xAnimal)
            gvCrias2.DataBind()
        End If
    End Sub

    Private Sub BorraEmpMov()
        Dim actualizado As Integer ' Numero de registros actualizados
        actualizado = TA_EmpMov.DQ_EmpMov(CType(xNumEmp, Integer))
        txtSalida.Text = actualizado.ToString + " REGISTRO BORRADO"
    End Sub

    Protected Sub txtToro_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtToro.TextChanged
        Dim xCadena As String = txtToro.Text
        Dim xlong_cadena As Integer
        xlong_cadena = xCadena.Length
        If xlong_cadena <= 1 Then
            txtSalida.Text = "Error Cadena insuficiente"
        Else
            'xCadena = xCadena.Remove(xlong_cadena - 1, 1)
            xCadena = xCadena.Remove(0, 1)
            txtSalida.Text = xCadena
        End If
    End Sub

    Protected Sub txtFIT_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFIT.TextChanged
        If txtFFT.Text.Length > 0 And txtFIT.Text.Length > 0 Then
            If CDate(txtFIT.Text) > CDate(txtFFT.Text) Then 'Fecha inicio de Toro no puede ser mayor que fin de Toro
                txtFIT.Text = ""
            End If
        End If
    End Sub

    Protected Sub txtFFT_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFFT.TextChanged
        If txtFIT.Text.Length > 0 And txtFFT.Text.Length > 0 Then
            If CDate(txtFIT.Text) > CDate(txtFFT.Text) Then 'Fecha fin de Toro no puede ser menor que inicio de Toro
                txtFFT.Text = ""
            End If
        End If
    End Sub

    Protected Sub txtFIV_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFIV.TextChanged
        If txtFIT.Text.Length > 0 Then
            If CDate(txtFIV.Text) < CDate(txtFIT.Text) Then 'Para inicio de la Vaca, no puede ser menor que inicio de Toro
                txtFIV.Text = ""
            End If
        End If
        If txtFFT.Text.Length > 0 And txtFIV.Text.Length > 0 Then
            If CDate(txtFIV.Text) > CDate(txtFFT.Text) Then 'Para inicio de la Vaca no puede ser mayor que fin de Toro
                txtFIV.Text = ""
            End If
        End If
        If txtFFV.Text.Length > 0 And txtFIV.Text.Length > 0 Then
            If CDate(txtFIV.Text) > CDate(txtFFV.Text) Then 'Fecha inicio de Vaca no puede ser mayor que fin de Vaca
                txtFIV.Text = ""
            End If
        End If
    End Sub

    Protected Sub txtFFV_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFFV.TextChanged
        If txtFIT.Text.Length > 0 Then
            If CDate(txtFFV.Text) < CDate(txtFIT.Text) Then 'Para fin de la Vaca, no puede ser menor que inicio de Toro
                txtFFV.Text = ""
            End If
        End If
        If txtFFT.Text.Length > 0 And txtFFV.Text.Length > 0 Then
            If CDate(txtFFV.Text) > CDate(txtFFT.Text) Then 'Para fin de la Vaca no puede ser mayor que fin de Toro
                txtFFV.Text = ""
            End If
        End If
        If txtFIV.Text.Length > 0 And txtFFV.Text.Length > 0 Then
            If CDate(txtFIV.Text) > CDate(txtFFV.Text) Then 'Fecha fin de Vaca no puede ser menor que inicio de Vaca
                txtFFV.Text = ""
            End If
        End If
    End Sub

    Protected Sub chkPropT_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkPropT.CheckedChanged
        If chkPropT.Checked Then
            DT_Animal = TA_Animal.GetAniEmpByToro1(ddlEdadT.SelectedValue, CType(ddlAno.SelectedValue, Integer))
            gvToros.DataSource = DT_Animal.Select()
            gvToros.DataBind()
        Else
            DT_Animal = TA_Animal.GetAniEmpByToro2(ddlEdadT.SelectedValue, CType(ddlAno.SelectedValue, Integer))
            gvToros.DataSource = DT_Animal.Select()
            gvToros.DataBind()
        End If
    End Sub

    Protected Sub chkPropV_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkPropV.CheckedChanged
        If chkPropV.Checked Then
            DT_Animal = TA_Animal.GetAniEmpByVaca1(ddlEdadV.SelectedValue, CType(ddlAno.SelectedValue, Integer))
            gvVacas.DataSource = DT_Animal.Select()
            gvVacas.DataBind()
        Else
            DT_Animal = TA_Animal.GetAniEmpByVaca2(ddlEdadV.SelectedValue, CType(ddlAno.SelectedValue, Integer))
            gvVacas.DataSource = DT_Animal.Select()
            gvVacas.DataBind()
        End If
    End Sub

    Protected Sub chkItemSelect1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rbActivo As Boolean = False
        For Each row In gvVacas.Rows
            Dim cb As CheckBox = row.FindControl("chkItemSelect1")
            If cb.Checked Then
                rbActivo = True
            End If
        Next
    End Sub

    Protected Sub chkSelectAll1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rbActivo As Boolean = False
        Dim chkSeleccionado As CheckBox = gvVacas.HeaderRow.FindControl("chkSelectAll1")
        For Each row In gvVacas.Rows
            Dim chk As CheckBox = row.FindControl("chkItemSelect1")
            chk.Checked = chkSeleccionado.Checked
            If chk.Checked Then
                rbActivo = True
            End If
        Next
        'rbActivo1.Checked = rbActivo
    End Sub

    'Protected Sub btnAgregaV_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregaV.Click
    'xStatus = "A"
    'lblNumEmpT.Text = lblNumEmpV.Text
    'CargaEmpMov()
    'End Sub

    'Protected Sub txtToro9_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtToro9.TextChanged
    'DT_Empadre = Nothing
    'gvEmpadre.DataSource = Nothing
    'gvEmpadre.DataBind()

    'gvEmpadre.DataSource = TA_Empadre.GetEmpadreByToro("_" & txtToro9.Text & "%")
    'gvEmpadre.DataBind()
    'txtUbicacion9.Text = ""
    'txtEstatus.Text = ""
    'txtToro9.Focus()
    'gvEmpMov.DataSource = Nothing
    'gvEmpMov.DataBind()
    ''txtSalida.Text = gvEmpadre.SelectedRow.Cells(1).Text
    'End Sub
End Class
